/**
 * futures : masterlink.futures.controller.IndexController.java
 */
package masterlink.futures.controller;

import javax.servlet.http.HttpServletRequest;

import masterlink.futures.util.WebUtils;
import masterlink.futures.vo.Account;
import masterlink.futures.vo.Employee;
import masterlink.futures.vo.Function;
import masterlink.futures.vo.Group;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * TODO Write the description
 *
 * @author : tonyyang
 * Create date : 2011/9/30
 * @version 1.0.0	
 */
@Controller
@RequestMapping(value = "/data")
public class DataController extends BasicController{
	
	@RequestMapping(method=RequestMethod.GET)
	@Override
	public String index(HttpServletRequest req){
		Employee emp=WebUtils.getLogEmployee(req);
		Function fun=WebUtils.getFunction(req);
		log.trace("Employee {} is forwarded to {}",emp,fun.getUrl());
		return "forward:"+fun.getUrl();
	}
	
	@RequestMapping(value = "/group/{gid}",method=RequestMethod.GET)
	public String group(@PathVariable("gid")String gid,HttpServletRequest req){
		log.trace("Loading data for Gid {}",gid);
		Employee emp=WebUtils.getLogEmployee(req);
		Function fun=WebUtils.getFunction(req);
		WebUtils.setGroup(WebUtils.findGroup(emp.getAllGroups(),gid), req);
		WebUtils.setAccount(null, req);
		log.trace("Employee {} is forwarded to {}",emp,fun.getUrl());
		return "forward:"+fun.getUrl();
	}
	
	@RequestMapping(value = "/acc/{gid}/{acno}",method=RequestMethod.GET)
	public String acc(@PathVariable("gid")String gid,@PathVariable("acno")String acno,HttpServletRequest req){
		log.trace("Loading data for Gid {}, acno {}",gid,acno);
		Employee emp=WebUtils.getLogEmployee(req);
		Function fun=WebUtils.getFunction(req);
		Group g=WebUtils.findGroup(emp.getAllGroups(), gid);
		Account acc=WebUtils.findAccount(g.getAccounts(), acno);
		WebUtils.setGroup(g, req);
		WebUtils.setAccount(acc, req);
		log.trace("Employee {} is forwarded to {}",emp,fun.getUrl());
		return "forward:"+fun.getUrl();
	}
	
	@RequestMapping(value = "/fun/{id}",method=RequestMethod.GET)
	public String fun(@PathVariable("id")String id,HttpServletRequest req){
		Employee emp=WebUtils.getLogEmployee(req);
		log.trace("{} Loading function for id {}",emp,id);
		Function fun=WebUtils.findFunction(emp.getAllFun(), id);
		WebUtils.setFunction(fun, req);
		log.trace("Employee {} is forwarded to {}",emp,fun.getUrl());
		return "forward:"+fun.getUrl();
	}
	
	@RequestMapping(value = "/index",method=RequestMethod.GET)
	public String index(){
		return "data/index";
	}
}
